package cn.com.gridinfo.sooc.core.course.directory.adapter;

import android.content.Context;
import android.util.SparseIntArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.List;

import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.core.course.common.ExtType;
import cn.com.gridinfo.sooc.core.course.directory.bean.MenuDisplay;


/**
 * 课程目录适配器
 */
public class PinnedHeaderExpandableAdapter extends BaseExpandableListAdapter implements PinnedHeaderExpandableListView.HeaderAdapter {
    private Context context;
    private PinnedHeaderExpandableListView listView;
    private LayoutInflater inflater;
    List<MenuDisplay> courseMenuList;

    private int gPosition = -1;//当前 parent 选中位置
    private int sPosition = -1;//当前childern选中位置

    private MenuDisplay nextMenuDisplay;//下一个播放素材
    private MenuDisplay firstMenuDisplay;//第一个播放素材
    private boolean isShowFreeStudy = false;//是否显示免费试学文字

    public PinnedHeaderExpandableAdapter(List<MenuDisplay> courseMenuList, Context context, PinnedHeaderExpandableListView listView) {
        this.courseMenuList = courseMenuList;
        this.context = context;
        this.listView = listView;
        inflater = LayoutInflater.from(this.context);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return courseMenuList.get(groupPosition).getSubList().get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {
        View view = null;
        if (convertView != null) {
            view = convertView;
        } else {
            view = createChildrenView();
        }
        RelativeLayout dic_child_layout = (RelativeLayout) view.findViewById(R.id.dic_child_layout);
        ImageView ic_img = (ImageView) view.findViewById(R.id.dic_nr_img);
        TextView text = (TextView) view.findViewById(R.id.dic_childName);
        TextView freeStudyText = (TextView) view.findViewById(R.id.free_study);
        View lineView = (View) view.findViewById(R.id.divide_line);
        //获取节或者素材信息
        MenuDisplay menuDisplay = courseMenuList.get(groupPosition).getSubList().get(childPosition);
        //设置节或者素材名称
        text.setText(menuDisplay.getMlmc());
        //节 显示分割线,没有图标
        if (menuDisplay.getMenuLevel() == 2) {
            dic_child_layout.getLayoutParams().height = context.getResources().getDimensionPixelSize(R.dimen.dir_parent_h);
            ic_img.setVisibility(View.GONE);
        }
        //素材 不显示分割线,显示图标
        if (menuDisplay.getMenuLevel() == 3) {
            dic_child_layout.getLayoutParams().height = context.getResources().getDimensionPixelSize(R.dimen.dir_child_h);
            ic_img.setVisibility(View.VISIBLE);

            String nrlx = menuDisplay.getNrlx().toLowerCase().trim();
            //获取播放素材类型
            String typeClass = ExtType.getExtTypeClass(nrlx);
            //设置视频类型,只要是视频无论内容类型是 avi mp4等其他统一视频图标

            setImageIcon(false, nrlx, ic_img);

        }
        //免费试学的显示
        if (isShowFreeStudy) {
            if (getFristSucai() != null && getFristSucai().getNrid().equals(menuDisplay.getNrid())) {
                freeStudyText.setVisibility(View.VISIBLE);
            } else {
                freeStudyText.setVisibility(View.GONE);
            }
        } else {
            freeStudyText.setVisibility(View.GONE);
        }
        //设置当前选中
        if (groupPosition == gPosition && childPosition == sPosition) {
            text.setTextColor(context.getResources().getColor(R.color.colorAccent));
            setImageIcon(true, menuDisplay.getNrlx().toLowerCase().trim(), ic_img);
        } else {
            if (menuDisplay.getRead().equals("1")) {
                text.setTextColor(context.getResources().getColor(R.color.color_tv333));
            } else {
                text.setTextColor(context.getResources().getColor(R.color.color_666));
            }
        }

        return view;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        if (courseMenuList != null && !courseMenuList.isEmpty()) {
            return courseMenuList.get(groupPosition).getSubList().size();
        }
        return 0;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return courseMenuList.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return courseMenuList.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return 0;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        View view = null;
        if (convertView != null) {
            view = convertView;
        } else {
            view = createGroupView();
        }


        TextView text = (TextView) view.findViewById(R.id.dic_parentName);
        text.setText(courseMenuList.get(groupPosition).getMlmc());
        return view;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    private View createChildrenView() {
        return inflater.inflate(R.layout.course_directory_child, null);
    }

    private View createGroupView() {
        return inflater.inflate(R.layout.course_directory_parent, null);
    }

    @Override
    public int getHeaderState(int groupPosition, int childPosition) {
        final int childCount = getChildrenCount(groupPosition);
        if (childPosition == childCount - 1) {
            return PINNED_HEADER_PUSHED_UP;
        } else if (childPosition == -1
                && !listView.isGroupExpanded(groupPosition)) {
            return PINNED_HEADER_GONE;
        } else {
            return PINNED_HEADER_VISIBLE;
        }
    }

    @Override
    public void configureHeader(View header, int groupPosition,
                                int childPosition, int alpha) {
        String groupData = this.courseMenuList.get(groupPosition).getMlmc();
        ((TextView) header.findViewById(R.id.dic_headName
        )).setText(groupData);

    }

    private SparseIntArray groupStatusMap = new SparseIntArray();

    @Override
    public void setGroupClickStatus(int groupPosition, int status) {
        groupStatusMap.put(groupPosition, status);
    }

    @Override
    public int getGroupClickStatus(int groupPosition) {
        if (groupStatusMap.keyAt(groupPosition) >= 0) {
            return groupStatusMap.get(groupPosition);
        } else {
            return 0;
        }
    }

    //寻找下一个素材，递归方法
    public void getNextMenuDisplay3(int gPosition, int currPositon) {
        int nextGposition = gPosition;
        int nextSubPosition = currPositon + 1;


        //当前父目录位置是否超过父目录的总数
        if (gPosition >= courseMenuList.size()) {
            this.nextMenuDisplay = null;
            return;
        }

        //获取当前父目录对象
        MenuDisplay nextParentMenu = courseMenuList.get(nextGposition);

        //当前子目录的位置是否在当前素材列表中存在
        if (currPositon >= nextParentMenu.getSubList().size()) {
            this.nextMenuDisplay = null;
            return;
        }
        MenuDisplay currSubMenu = nextParentMenu.getSubList().get(currPositon);//当前播放素材

        MenuDisplay nextSubMenu = null;
        //当前章内有下一个位置有数据
        if (nextSubPosition < nextParentMenu.getSubList().size()) {
            nextSubMenu = nextParentMenu.getSubList().get(nextSubPosition);//下一播放素材
            //当前素材为答题 -1 时 ，下一素材不可点击，只有答题答对了才可点击
//            if("-1".equals(currSubMenu.getNrlx())) {
//                //当前章下一个素材 变为可读
//                if(currSubMenu.getPass()==1) {
//                    nextSubMenu.setRead(1);
//                }
//            }
            //数据内容为素材
            if (3 == nextSubMenu.getMenuLevel()) {
                //设置当前父节点位置和当前父节点下的下一个素材位置

                this.nextMenuDisplay = nextSubMenu;
//
            } else {

                getNextMenuDisplay3(nextGposition, nextSubPosition);

            }

        }
        //当前章节下一位置没有数据
        else {
            //找下一个章节第一个素材
            nextGposition = gPosition + 1;
            nextSubPosition = 1;
//            nextParentMenu = courseMenuList.get(nextGposition);
            //有下一个章不为空，找下一个章是不是有第一个素材
            if (nextGposition < courseMenuList.size()) {


                if (nextSubPosition < courseMenuList.get(nextGposition).getSubList().size()) {
                    nextSubMenu = courseMenuList.get(nextGposition).getSubList().get(nextSubPosition);
//                    //当前素材为答题 -1 时 ，下一素材不可点击，只有答题答对了才可点击
                    //当前素材为答题 -1 时 ，下一素材不可点击，只有答题答对了才可点击
//                    if("-1".equals(currSubMenu.getNrlx())) {
//                        //当前章下一个素材 变为可读
//                        if(currSubMenu.getPass()==1) {
//                            nextSubMenu.setRead(1);
//                        }
//                    }
                    if (3 == nextSubMenu.getMenuLevel()) {
                        //设置下一素材播放位置返回
                        this.nextMenuDisplay = nextSubMenu;
                    } else {
                        getNextMenuDisplay3(nextGposition, nextSubPosition);
                    }
                } else {
                    this.nextMenuDisplay = null;
                }
            } else {
                this.nextMenuDisplay = null;
            }
        }
    }

    public MenuDisplay getNextMenuDisplay() {
        return nextMenuDisplay;
    }


    public void setNextMenuDisplay(MenuDisplay nextMenuDisplay) {
        this.nextMenuDisplay = nextMenuDisplay;
    }

    public void setImageIcon(boolean isLight, String nrlx, ImageView ic_img) {
        //设置视频类型,只要是视频无论内容类型是 avi mp4等其他统一视频图标
        if (nrlx.equals("video")) {
            if (isLight) {
                ic_img.setImageResource(R.mipmap.ext_video_light);
            } else {
                ic_img.setImageResource(R.mipmap.ext_video_grey);
            }
        }
        //设置 doc docx 文档
        else if (nrlx.equals("doc")) {
            if (isLight) {
                ic_img.setImageResource(R.mipmap.ext_doc_light);
            } else {
                ic_img.setImageResource(R.mipmap.ext_doc_grey);
            }
        }
        //设置 ppt pptx 文档
        else if (nrlx.equals("ppt")) {
            if (isLight) {
                ic_img.setImageResource(R.mipmap.ext_ppt_light);
            } else {
                ic_img.setImageResource(R.mipmap.ext_ppt_grey);
            }
        }
        //设置 gif 图片
        else if (nrlx.equals("photo")) {
            if (isLight) {
                ic_img.setImageResource(R.mipmap.ext_jpg_light);
            } else {
                ic_img.setImageResource(R.mipmap.ext_jpg_grey);
            }
        }
        //设置 习题 图片
        else if (nrlx.equals("-1")) {
            if (isLight) {
                ic_img.setImageResource(R.mipmap.ext_xiti_light);
            } else {
                ic_img.setImageResource(R.mipmap.ext_xiti_grey);
            }
        }
        //其他
        else {
            if (isLight) {
                ic_img.setImageResource(R.mipmap.ext_extra_lignt);
            } else {
                ic_img.setImageResource(R.mipmap.ext_extra_grey);
            }
        }

    }

    public void setSelectorRefresh(int gPosition, int sPosition) {
        this.gPosition = gPosition;
        this.sPosition = sPosition;

        notifyDataSetChanged();
    }

    //获取第一章第一节第一个素材 用于免费试学
    public MenuDisplay getFristSucai() {
        for (int i = 0; i < courseMenuList.get(0).getSubList().size(); i++) {
            if (courseMenuList.get(0).getSubList().get(i).getMenuLevel() == 3) {
                firstMenuDisplay = courseMenuList.get(0).getSubList().get(i);
                break;
            }
        }
        return firstMenuDisplay;
    }

    //获取列表数据
    public boolean isHasCourseSucai() {
        for (int i = 0; i < courseMenuList.size(); i++) {
            for (int j = 0; j < courseMenuList.get(i).getSubList().size(); j++) {
                if (courseMenuList.get(i).getSubList().get(j).getMenuLevel() == 3)
                    return true;
            }
        }
        return false;
    }


    //显示免费试学按钮
    public void showFreeStudyText() {
        isShowFreeStudy = true;
    }
}
